/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Desktop_Login.java
 *
 * Created on Mar 31, 2011, 9:39:05 PM
 */
package DesktopGUIpackage;

import Controller.DesktopController;
import Controller.DesktopControllerHelper;

/**
 *
 * @author edaype
 */
public class Desktop_LogIn extends javax.swing.JPanel {

    private DesktopController desktopController = null;
    private DesktopControllerHelper desktopControllerHelper = null;

    /** Creates new form Desktop_Login */
    public Desktop_LogIn(DesktopControllerHelper desktopControllerHelper) {
        this.desktopControllerHelper = desktopControllerHelper;
        desktopController = desktopControllerHelper.getDesktopController();
        initComponents();
        ErrorLabel.setText("");
        ErrorLabel.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        MainPanel = new javax.swing.JPanel();
        TitleLabel = new javax.swing.JLabel();
        StoreCardLabel = new javax.swing.JLabel();
        PasswordLabel = new javax.swing.JLabel();
        StoreCardField = new javax.swing.JTextField();
        PasswordField = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        TitlePanel = new javax.swing.JPanel();
        LogInButton = new javax.swing.JButton();
        RecoverButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        ErrorLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        MainPanel.setBackground(new java.awt.Color(255, 255, 255));

        TitleLabel.setFont(new java.awt.Font("Tahoma", 0, 48));
        TitleLabel.setText("Welcome back to DigiStore!");

        StoreCardLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        StoreCardLabel.setText("StoreCard Number");

        PasswordLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        PasswordLabel.setText("Password");

        StoreCardField.setFont(new java.awt.Font("Tahoma", 0, 24));

        PasswordField.setFont(new java.awt.Font("Tahoma", 0, 24));

        jLabel1.setFont(new java.awt.Font("Tahoma", 2, 14));
        jLabel1.setText("To retreive a your password click Recover");

        javax.swing.GroupLayout MainPanelLayout = new javax.swing.GroupLayout(MainPanel);
        MainPanel.setLayout(MainPanelLayout);
        MainPanelLayout.setHorizontalGroup(
            MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, MainPanelLayout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(TitleLabel)
                    .addComponent(StoreCardLabel)
                    .addComponent(StoreCardField, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
                    .addComponent(PasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
                    .addComponent(PasswordLabel)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(69, 69, 69))
        );
        MainPanelLayout.setVerticalGroup(
            MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MainPanelLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(TitleLabel)
                .addGap(32, 32, 32)
                .addComponent(StoreCardLabel)
                .addGap(26, 26, 26)
                .addComponent(StoreCardField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(PasswordLabel)
                .addGap(28, 28, 28)
                .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        TitlePanel.setBackground(new java.awt.Color(255, 255, 255));

        LogInButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        LogInButton.setText("Log In");
        LogInButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogInButtonActionPerformed(evt);
            }
        });

        RecoverButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        RecoverButton.setText("Recover");
        RecoverButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RecoverButtonActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 60));
        jLabel2.setText("Log In");

        javax.swing.GroupLayout TitlePanelLayout = new javax.swing.GroupLayout(TitlePanel);
        TitlePanel.setLayout(TitlePanelLayout);
        TitlePanelLayout.setHorizontalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 429, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 75, Short.MAX_VALUE)
                .addComponent(RecoverButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(LogInButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        TitlePanelLayout.setVerticalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TitlePanelLayout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addGroup(TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LogInButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RecoverButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(TitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        ErrorLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        ErrorLabel.setText("Error Label");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(ErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 516, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(MainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(MainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(ErrorLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                .addComponent(TitlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void LogInButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LogInButtonActionPerformed
        // TODO add your handling code here:
        String name = StoreCardField.getText();
        boolean isValid = false;
        isValid = validateFields();

        if(isValid){

                try {
                Integer.parseInt(name);
                String password = new String(PasswordField.getPassword());

                //      System.out.println("name = " + name + "   password = " + password);
                if (!desktopController.LogIn(name, password)) {
                    //        System.out.println("unsuccessful from login.java");
                    ErrorLabel.setText(
                            "Store Card Number / Password combination not valid");
                    ErrorLabel.setVisible(true);
                } else {
                    //        System.out.println("successful from login.java");
                    //desktopController.getSessionMgr().setOnPathToPurchase(true);
                    if (desktopController.getSessionMgr().isOnPathToPurchase()) {
                        desktopControllerHelper.changeFrame(11);
                    } else {
                        desktopControllerHelper.changeFrame(2);
                    }
                }
            } catch (NumberFormatException nfe) {
                ErrorLabel.setText(
                        "Store Card Number should be numeric");
                ErrorLabel.setVisible(true);
            }
        }
        

}//GEN-LAST:event_LogInButtonActionPerformed

    private void RecoverButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RecoverButtonActionPerformed
        // TODO add your handling code here:
        desktopControllerHelper.changeFrame(4);

    }//GEN-LAST:event_RecoverButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ErrorLabel;
    private javax.swing.JButton LogInButton;
    private javax.swing.JPanel MainPanel;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JLabel PasswordLabel;
    private javax.swing.JButton RecoverButton;
    private javax.swing.JTextField StoreCardField;
    private javax.swing.JLabel StoreCardLabel;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JPanel TitlePanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables

    private boolean validateFields() {

            boolean isValid = true;

            if("".equalsIgnoreCase(StoreCardField.getText().trim())){
                isValid = false;
                ErrorLabel.setText(" store card is mandatory");
                ErrorLabel.setVisible(true);
                //return isValid;
            }else if(PasswordField.getPassword().length == 0){
                isValid = false;
                ErrorLabel.setText("password is mandatory");
                 ErrorLabel.setVisible(true);
                //return isValid;
            }
            return isValid;


    }
}
